package com.login.controller;

import com.login.pojo.User;
import com.login.service.UserService;
import com.login.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;


@RestController("controllerForLogin")
@RequestMapping("users")
public class UserController extends BaseController{

    @Autowired
 private UserService userService;

    @PostMapping("reg")

    public JsonResult<Void> reg(User user)  {
        userService.reg(user);
        return new JsonResult<>(SUCCESS);
    }

    @GetMapping("login")

    public JsonResult<User> login(String username, String password,Integer type, HttpSession session){
        User data = userService.login(username,password,type);
        session.setAttribute("username",data.getUsername());
        session.setAttribute("password",data.getPassword());
        session.setAttribute("type",data.getType());
        return new JsonResult <>(SUCCESS,data);
    }
}

